package client.controleur;

import java.io.Serializable;
import java.rmi.RemoteException;

import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import client.vue.CreerPartieReseau;
import codebase.IPartie;

public class CreerPartieReseauxChangeListener implements ChangeListener, Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CreerPartieReseau fenetre;
	private IPartie partie;

	public CreerPartieReseauxChangeListener(CreerPartieReseau fenetre, IPartie partie) {
		this.fenetre = fenetre;
		this.partie = partie;
	}

	@Override
	public void stateChanged(ChangeEvent arg0) {
		if (arg0.getSource() instanceof JSlider) {
			JSlider silder = (JSlider) arg0.getSource();
			try {
				if (partie.getConfig().getNbJoueurs() != (Integer) silder.getValue()) {
					partie.setConfigAttribute(0, silder.getValue());
				}
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		} else if (arg0.getSource() instanceof JSpinner) {
			JSpinner spinner = (JSpinner) arg0.getSource();
			if (spinner.getName().equals("dureePartieTps")) {
				try {
					if (partie.getConfig().getDureePartieTps() != (Integer) spinner.getValue()) {
						partie.setConfigAttribute(2, (Integer) spinner.getValue());
					}
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
			} else if (spinner.getName().equals("dureePartieNbAnnees")) {
				try {
					if (partie.getConfig().getDureePartieNbAnnees() != (Integer) spinner.getValue()) {
						partie.setConfigAttribute(3, (Integer) spinner.getValue());
					}
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
			} else if (spinner.getName().equals("nbPtsRavitallement")) {
				try {
					if (partie.getConfig().getNbCentresRavitallementVictoire() != (Integer) spinner
							.getValue()) {
						partie.setConfigAttribute(1, (Integer) spinner.getValue());
					}
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
			} else if (spinner.getName().equals("dureeNegociations")) {
				try {
					if (partie.getConfig().getDureeNegociations() != (Integer) spinner.getValue()) {
						partie.setConfigAttribute(4, (Integer) spinner.getValue());
					}
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
			} else if (spinner.getName().equals("dureeOrdres")) {
				try {
					if (partie.getConfig().getDureeOrdres() != (Integer) spinner.getValue()) {
						partie.setConfigAttribute(5, (Integer) spinner.getValue());
					}
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
			} else if (spinner.getName().equals("dureeDispersions")) {
				try {
					if (partie.getConfig().getDureeDispersions() != (Integer) spinner.getValue()) {
						partie.setConfigAttribute(6, (Integer) spinner.getValue());
					}
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
			}
		}
	}
}
